#include "layermanager.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <QFile>
#include <iostream>

namespace Graph {

static unsigned char ColorGrah[15][15][3] =
{
    //"Алый": 1
    {
        { 255,   0,  85 },
        { 179,   0,  59 },
        { 131,   0,  43 },
        {  91,   0,  30 },
        {  54,   0,  18 },
        { 255,  79, 138 },
        { 179,  55,  96 },
        { 131,  41,  71 },
        {  91,  28,  49 },
        { 255, 124, 168 },
        { 179,  87, 118 },
        { 131,  64,  86 },
        { 255, 162, 193 },
        { 179, 114, 135 },
        { 255, 195, 215 }
    },


    // "Малиновый": 2

    {
        { 255,   0, 162 },
        { 178,   0, 113 },
        { 130,   0,  82 },
        {  89,   0,  56 },
        {  52,   0,  33 },
        { 255,  79, 191 },
        { 178,  55, 133 },
        { 130,  40,  97 },
        {  89,  27,  67 },
        { 255, 124, 207 },
        { 178,  87, 145 },
        { 130,  63, 106 },
        { 255, 162, 221 },
        { 178, 113, 154 },
        { 255, 195, 233 }
    },


    //"Розовый": 3
    {

        { 255,   0, 255 },
        { 177,   0, 177 },
        { 129,   0, 129 },
        {  87,   0,  87 },
        {  50,   0,  50 },
        { 255,  79, 255 },
        { 177,  55, 177 },
        { 129,  40, 129 },
        {  87,  27,  87 },
        { 255, 124, 255 },
        { 177,  86, 177 },
        { 129,  63, 129 },
        { 255, 162, 255 },
        { 177, 113, 177 },
        { 255, 195, 255 }
    },


    //  "Фиолетовый": 4

    {
        { 170,   0, 255 },
        { 119,   0, 179 },
        {  88,   0, 132 },
        {  61,   0,  92 },
        {  37,   0,  56 },
        { 196,  79, 255 },
        { 138,  56, 179 },
        { 102,  41, 132 },
        {  71,  28,  92 },
        { 211, 124, 255 },
        { 149,  88, 179 },
        { 110,  65, 132 },
        { 224, 162, 255 },
        { 158, 114, 179 },
        { 235, 195, 255 }
    },


    //   "Ультрамарин": 5

    {
        {  98,   0, 255 },
        {  70,   0, 182 },
        {  52,   0, 136 },
        {  37,   0,  98 },
        {  24,   0,  63 },
        { 146,  79, 255 },
        { 105,  56, 182 },
        {  78,  42, 136 },
        {  56,  30,  98 },
        { 174, 124, 255 },
        { 124,  89, 182 },
        {  93,  67, 136 },
        { 198, 162, 255 },
        { 141, 116, 182 },
        { 218, 195, 255 }
    },

    // "Синий": 6

    {
        {   0,   0, 255 },
        {   0,   0, 187 },
        {   0,   0, 145 },
        {   0,   0, 109 },
        {   0,   0,  76 },
        {  79,  79, 255 },
        {  58,  58, 187 },
        {  45,  45, 145 },
        {  34,  34, 109 },
        { 124, 124, 255 },
        {  91,  91, 187 },
        {  71,  71, 145 },
        { 162, 162, 255 },
        { 119, 119, 187 },
        { 195, 195, 255 }
    },


    // "Голубой": 7

    {
        {   0, 145, 255 },
        {   0, 100, 176 },
        {   0,  72, 128 },
        {   0,  49,  86 },
        {   0,  27,  48 },
        {  79, 179, 255 },
        {  55, 124, 176 },
        {  39,  90, 128 },
        {  26,  60,  86 },
        { 124, 198, 255 },
        {  86, 137, 176 },
        {  62,  99, 128 },
        { 162, 215, 255 },
        { 112, 149, 176 },
        { 195, 229, 255 }
    },


    // "Бирюзовый": 8

    {
        {   0, 255, 255 },
        {  79, 255, 255 },
        { 124, 255, 255 },
        { 162, 255, 255 },
        { 195, 255, 255 },
        {   0, 173, 173 },
        {  62, 173, 173 },
        {  97, 173, 173 },
        { 127, 173, 173 },
        {   0, 121, 121 },
        {  53, 121, 121 },
        {  83, 121, 121 },
        {   0,  78,  78 },
        {  44,  78,  78 },
        {   0,  38,  38 }
    },


    //"Аквамарин":  9

    {
        {   0, 255, 170 },
        {  79, 255, 196 },
        { 124, 255, 211 },
        { 162, 255, 224 },
        { 195, 255, 235 },
        {   0, 173, 115 },
        {  62, 173, 136 },
        {  98, 173, 148 },
        { 127, 173, 158 },
        {   0, 122,  81 },
        {  53, 122,  99 },
        {  83, 122, 109 },
        {   0,  79,  52 },
        {  44,  79,  67 },
        {   0,  40,  26 }
    },


   // "Зеленый": 10
    {
        {   0, 255,   0 },
        {  79, 255,  79 },
        { 124, 255, 124 },
        { 162, 255, 162 },
        { 195, 255, 195 },
        {   0, 174,   0 },
        {  62, 174,  62 },
        {  98, 174,  98 },
        { 128, 174, 128 },
        {   0, 124,   0 },
        {  54, 124,  54 },
        {  84, 124,  84 },
        {   0,  81,   0 },
        {  46,  81,  46 },
        {   0,  42,   0 },
    },

    //"Лайм": 11
    {
        { 196,  255,  0},
        { 214,  255,  79},
        { 224,  255,  124},
        { 233,  255,  162},
        { 241,  255,  195},
        { 131,  171,  0},
        { 146,  171,  61},
        { 154,  171,  97},
        { 161,  171,  126},
        {  91,  119,  0},
        { 104,  119,  52},
        { 110,  119,  81},
        {  57,   75,  0},
        {  67,   75,  42},
        {  27,   35,  0}
    },


    //"Желтый": 12
    {
        { 255,  255,  0},
        { 255,  255,  79},
        { 255,  255,  124},
        { 255,  255,  162},
        { 255,  255,  195},
        { 170,  170,  0},
        { 170,  170,  61},
        { 170,  170,  96},
        { 170,  170,  125},
        { 118,  118,  0},
        { 118,  118,  51},
        { 118,  118,  80},
        { 73,  73,  0},
        { 73,  73,  41},
        { 33,  33,  0}
    },


    //"Золотой": 13
    {

        { 255,  170,  0},
        { 255,  196,  79},
        { 255,  211,  124},
        { 255,  224,  162},
        { 255,  235,  195},
        { 173,  115,  0},
        { 173,  136,  62},
        { 173,  148,  98},
        { 173,  157,  127},
        { 122,  81,  0},
        { 122,  99,  53},
        { 122,  109,  83},
        { 78,  52,  0},
        { 78,  67,  44},
        { 39,  26,  0}
    },


    // "Оранжевый": 14
    {
        { 255,  94,  0},
        { 255,  144,  79},
        { 255,  172,  124},
        { 255,  196,  162},
        { 255,  217,  195},
        { 175,  64,  0},
        { 175,  104,  63},
        { 175,  127,  99},
        { 175,  146,  129},
        { 126,  46,  0},
        { 126,  81,  54},
        { 126,  100,  86},
        { 83,  30,  0},
        { 83,  60,  47},
        { 45,  16,  0}
    },


    // "Красный": 15

    {
        { 255,   0,   0 },
        { 255,  79,  79 },
        { 255, 124, 124 },
        { 255, 162, 162 },
        { 255, 195, 195 },
        { 180,   0,   0 },
        { 180,  64,  64 },
        { 180, 101, 101 },
        { 180, 132, 132 },
        { 133,   0,   0 },
        { 133,  57,  57 },
        { 133,  90,  90 },
        {  93,   0,   0 },
        {  93,  52,  52 },
        {  57,   0,   0 }
    },
};

LayerManager::LayerManager()
{
//    QFile file(fileName);

//    char buffer[10000] = {};
//    if (file.exists())
//    {
//        file.open(QIODevice::ReadOnly);
//        file.read(buffer, 10000);
//        file.close();
//    }

//    document.Parse(buffer, 10000);
//    if (!document.IsObject())
//    {
//        document.SetObject();
//    }
//    if (!document.HasMember("CurrentLayer") &&
//            !document["CurrentLayer"].IsInt())
//    {
//        document
//    }
//    currentLayer = document["CurrentLayer"].GetInt();

//    char layerName[50];
//    sprintf(layerName, "%s%d", "Layer", currentLayer);
//    if (!document.HasMember(layerName) &&
//            !document[layerName].IsObject())
//    {
//        return;
//    }

//    rapidjson::StringBuffer buf;
//    rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
//    document.Accept(writer);

}

GraphSettings LayerManager::GetGraphSettings(const QString &name)
{
    GraphSettings result;

    Q_UNUSED(name);

    result.color.setRgb(
            ColorGrah[counter / 15 % 15][counter % 15][0],
            ColorGrah[counter / 15 % 15][counter % 15][1],
            ColorGrah[counter / 15 % 15][counter % 15][2]);

    result.visible = false;
    counter++;

    return result;
}

int LayerManager::counter = 0;

}


